package com.clstu.homework;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Homework02SenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9999);
        //假定DatagramPacket对象可以收到1024大小的数据,其实UPD最大可以接受64*1024
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要问的问题");
        String s = scanner.next();
        //封包到DatagramPacket对象
        byte[] data = s.getBytes();
        DatagramPacket packet1 = new DatagramPacket(data, data.length, InetAddress.getByName("192.168.1.5"), 8888);
        socket.send(packet1);//发送
        socket.receive(packet);

        int readLen = packet.getLength();//拆包
        data = packet.getData();
        System.out.println(new String(data,0,readLen));
        socket.close();
    }
}
